package com.laoniu.controller.sys;

import com.laoniu.annotation.OperateLog;
import com.laoniu.biz.UserBiz;
import com.laoniu.dto.PageResponse;
import com.laoniu.dto.RestResult;
import com.laoniu.dto.request.UserListDto;
import com.laoniu.dto.request.UserRequestDto;
import com.laoniu.dto.response.UserResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserBiz userBiz;

    /**
     * 添加用户
     * @param dto
     * @return
     */
    @PostMapping("add")
    @OperateLog(bizName = "用户管理", operateType = "新增用户", operateDesc = "后台添加用户")
    public RestResult addUser(@RequestBody UserRequestDto dto){
        userBiz.addUser(dto);
        return RestResult.success();
    }

    /**
     * 获取用户id
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    public RestResult getById(@PathVariable Long id){
        return RestResult.success(userBiz.getById(id));
    }

    /**
     * 更新用户
     * @param dto
     * @return
     */
    @PostMapping("update")
    @OperateLog(bizName = "用户管理", operateDesc = "后台修改用户", operateType = "修改用户")
    public RestResult updateUser(@RequestBody UserRequestDto dto){
        userBiz.updateUser(dto);
        return RestResult.success();
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    @OperateLog(bizName = "用户管理", operateType = "删除用户", operateDesc = "后台删除用户")
    public RestResult deleteUSer(@PathVariable Long id){
        userBiz.deleteUser(id);
        return RestResult.success();
    }

    /**
     * 用户列表
     * @param dto
     * @return
     */
    @PostMapping("list")
    public RestResult list(@RequestBody UserListDto dto){
        PageResponse<UserResponseDto> page = userBiz.pageList(dto);
        return RestResult.success(page);
    }

    /**
     * 用户角色
     * @param dto
     * @return
     */
    @PostMapping("userRoles")
    public RestResult userRoles(@RequestBody UserRequestDto dto){
        return RestResult.success(userBiz.userRoles(dto.getId()));
    }
}
